<?php
###########################################################################
#  Copyright (C) 2006-2007 Glyphix, Inc. <briz@glyphix.com>               #
#                                                                         #
#  Permission is hereby granted, free of charge, to any person obtaining  #
#  a copy of this software and associated documentation files (the        #
#  "Software"), to deal in the Software without restriction, including    #
#  without limitation the rights to use, copy, modify, merge, publish,    #
#  distribute, sublicense, and/or sell copies of the Software, and to     #
#  permit persons to whom the Software is furnished to do so, subject to  #
#  the following conditions:                                              #
#                                                                         #
#  The above copyright notice and this permission notice shall be         #
#  included in all copies or substantial portions of the Software.        #
#                                                                         #
#  Except as contained in this notice, the name(s) of the above           #
#  copyright holders shall not be used in advertising or otherwise to     #
#  promote the sale, use or other dealings in this Software without       #
#  prior written authorization.                                           #
#                                                                         #
#  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,        #
#  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF     #
#  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. #
#  IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR      #
#  OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,  #
#  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR  #
#  OTHER DEALINGS IN THE SOFTWARE.                                        #
###########################################################################

/**
 * GXPage_SubClass
 *
 * Common functions for GXPage subclasses such as GXPage_Page or GXPage_Action.
 * @author		Brad Brizendine <briz@glyphix.com>
 * @link		http://www.glyphix.com/
 * @license		http://opensource.org/licenses/bsd-license.php BSD License
 * @version		1.0
 * @package		GXPage
 * @subpackage	Engine
 * @copyright	Copyright 2006 - 2007, Glyphix, Inc.
 * @uses		SingletonPattern
 */

abstract class GXPage_SubClass extends SingletonPattern {

	// flag for changed data
	public $tainted = false;

	// the raw php native config
	public $raw;

	// the xml config for this object
	public $config;

	// the xml result for this object (Page will be the xml page, Action will be the result from Library function)
	public $result;

	public function __construct(){
		return true;
	}

	// runs the provided xpath on the object's config
	public function xpath( $xpath = null, $index = null, $engine = GXDOM::AS_GXDOM ){
		if( $this->config ){
			return $this->getConfig()->exec->xpath( $xpath, $index, $engine );
		}
		return false;
	}

	// returns whatever it can find in the raw php native config
	public function __get( $name = null ){
		if( !isset($this->raw[$name]) ){
			$val = $this->xpath($name,0);
			return ( is_object($val) ? $val->nodeValue : null );
		}else{
			return $this->raw[$name];
		}
	}

	// magic set method
	public function __set( $name = null, $value = null ){
		$this->raw[$name] = $value;
		// mark data as having changed
		$this->tainted = true;
		return true;
	}

	// sets the object's config, turning it into simplexml
	public function setConfig( $conf = null ){
		// if it's an object, assume it's GXDOM
		if( is_object($conf) ){
			$this->config = $conf;
		}else{
			$this->raw = $conf;
		}
		return true;
	}

	// returns the object's xml config ... if there's a value for @format, return its simplexml object; otherwise, return its GXDOM object
	public function getConfig( $format = null ){
		// make sure config is GXDOM
		if( !isset($this->config) || $this->tainted ){
			// all the classes that extend SubClass start with GXPage_, so remove GXPage_ to leave us with Params, Page, Action, SiteConfig, etc.
			$this->config = new GXDOM( str_replace('GXPage_','',get_class($this)), $this->raw );
		}
		// if we have no format, just return config
		return (($format) ? simplexml_import_dom($this->config->doc) : $this->config);
	}

	// returns the object's result
	public function getResult(){
		return $this->result;
	}

}

?>